package com.dyw.demoprovider.mq.consumer;


import com.dyw.demoapi.dto.MailDTO;
import com.dyw.demoapi.entity.MailUser;
import lombok.extern.log4j.Log4j2;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Devil
 * @create 2022-03-05 16:43
 */
@Component
@Log4j2
@RocketMQMessageListener(consumeMode = ConsumeMode.CONCURRENTLY, consumerGroup = "group2", messageModel = MessageModel.CLUSTERING, topic = "notice", selectorType = SelectorType.TAG, selectorExpression = "mail", nameServer = "localhost:9876")
public class Consumer implements RocketMQListener<MailDTO> {
    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private TemplateEngine templateEngine;

    @Autowired
    private JavaMailSenderImpl mailSender;


    @Override
    public void onMessage(MailDTO message) {
        log.info("消息接收成功");
        String sendTime = message.getSendTime();
        String content = message.getContent();
        String subject = message.getSubject();
        List<MailUser> userList = message.getUserList();
        try {
            Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(sendTime);
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    try {
                        mailSender.setUsername(from);
                        MimeMessage mimeMessage = mailSender.createMimeMessage();
                        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
                        mimeMessageHelper.setFrom(from);
                        mimeMessageHelper.setSubject(subject);
                        mimeMessageHelper.setSentDate(new Date());
                        for (MailUser mailUser : userList) {
                            String email = mailUser.getEmail();
                            String name = mailUser.getName();
                            mimeMessageHelper.setTo(email);
                            Context context = new Context();
                            context.setVariable("name", name);
                            context.setVariable("content", content);
                            context.setVariable("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
                            String process = templateEngine.process("Mail.html", context);
                            mimeMessageHelper.setText(process, true);
                            mailSender.send(mimeMessage);
                            log.info("发送成功 date={}", new Date());
                        }
                    } catch (MessagingException e) {
                        e.printStackTrace();
                    }
                }
            }, date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}
